/**
 * Created by zero on 2016/1/23.
 */
angular.module('meBindMemberControllers',[])
.controller('BindMemberCtrl',function($scope,$state,$timeout,$stateParams,ConvertedMsg,Storage,Member,BindMember,Loading,Toast,History){

    //定义注册会员对象
    $scope.member = {
      username: '',
      password: '',
      repassword: '',
      verifyCode: '',
      agreeService: false,
      memberId : 0
    };

    $scope.verifyCodFromBack = "";
    $scope.verifyCodeBtnText = "获取验证码";
    $scope.countdown = 60;

    $scope.$on('$ionicView.beforeEnter',function(){
      $scope.member.memberId = Storage.get("member").member_id;
    });


    //清空数据
    function clearData() {
      $scope.member.password = '';
      $scope.member.repassword = '';
      $scope.member.verifyCode = '';
      $scope.member.agreeService = false;
    }

    //服务协议
    $scope.go2Procotol = function () {
      $state.go("procotol");
    };

    //点击“服务协议”
    $scope.clickAgreeService = function () {
      $scope.member.agreeService = !$scope.member.agreeService;
    };

    //点击发送验证码
    $scope.clickGetVerifyCode = function () {
      var reg = /[1][3,4,5,8][0-9]{9}/;
      if ($scope.member.username == "") {
        Toast.showMessage("请输入手机号码，再点击获取验证码");
      }
      else if(!$scope.member.username.match(reg)){
        Toast.showMessage("手机号码格式不正确，请重新输入");
      }
      else{
        //获取验证码
        Member.getVerifyCode($scope.member.username);
        $scope.isBtnDisabled = true;
        //设置定时器
        var myTime = setInterval(function () {
          $scope.verifyCodeBtnText = ($scope.countdown--) + '秒';
          $scope.$digest(); // 通知视图模型的变化
        }, 1000);

        // 停止定时器
        setTimeout(function () {
          clearInterval(myTime);
          $scope.countdown = 60;
          $scope.verifyCodeBtnText = "重新获取验证码";
          $scope.isBtnDisabled = false;
          $scope.$digest(); // 通知视图模型的变化
        }, 61000);
      }
    };


    $scope.$on('Member.getVerifyCode', function () {
      var result = Member.getVerifyCodeInfo();
      console.log(result);
      if (result.resultStatus) {
        $scope.verifyCodFromBack = result.verifyCode;
      } else {
        Toast.showMessage(result.resultMsg);
      }
    });

    //用户绑定
    $scope.userBind = function () {
      var reg = /[1][3,4,5,8][0-9]{9}/;
      if ($scope.member.username == '') {
        Toast.showMessage("手机号码不能为空");
       /* $ionicLoading.show({
          noBackdrop: true,
          template: '手机号码不能为空',
          duration: 1500
        });*/
      } else if ($scope.member.password == '') {
        Toast.showMessage("密码不能为空");
      } else if (!$scope.member.username.match(reg)) {
        Toast.showMessage("手机号码格式不正确，请重新输入");
      } else if ($scope.member.repassword == '') {
        Toast.showMessage("确认密码不能为空");
      } else if ($scope.member.verifyCode == '') {
        Toast.showMessage("验证码不能为空");
      } else if ($scope.member.password != $scope.member.repassword) {
        Toast.showMessage("两次输入的密码不一致");
      } else if ($scope.member.verifyCode != $scope.verifyCodFromBack) {
        Toast.showMessage("验证码不正确");
      } else if ($scope.member.agreeService == false) {
        Toast.showMessage("请同意服务协议");
      } else {
        //进行账号绑定
        BindMember.bindMemberAccount($scope.member);
        $scope.verifyCodFromBack = "";
        Loading.showLoading();
        $timeout(function () {
          Loading.hideLoading();
          if (!$scope.networkOk) {
            Toast.showMessage("网络异常，请稍后再试....");
            clearData();
          }
        }, 5000);
      }
    };
    //当service访问网络成功时，调用该方法
    $scope.$on('BindMember.bindMember', function () {
      $scope.networkOk = true;
      Loading.hideLoading();
      var result = BindMember.getBindMemberInfo();
      if (result.bindStatus == false) {//绑定失败
        Toast.showMessage(result.bindMsg);
        clearData();
        $state.go('login');
      } else {
        $state.go('tab.discoverIndex');  //路由跳转到发现首页
      }
    });

    $scope.goBackToLogin = function(){
      History.goback();
    };

    //页面离开的时候调用
    $scope.$on('$ionicView.beforeLeave',function(){
      //删除对应ID的账号
      BindMember.deleteAccount($scope.member.memberId);
    });


  });
